//
//  UIViewController+CurrentController.m
//  MSJD
//
//  Created by 箭军刘 on 2018/10/15.
//  Copyright © 2018年 xiangbo. All rights reserved.
//

#import "UIViewController+CurrentController.h"

#import "MSMainViewController.h"
#import "AppDelegate.h"

@implementation UIViewController (CurrentController)

+ (UIViewController *)topViewController:(UIViewController*)vc {
    if (vc.presentedViewController) {
        return [UIViewController topViewController:vc.presentedViewController];
    } else if ([vc isKindOfClass:[UISplitViewController class]]) {
        UISplitViewController *svc = (UISplitViewController*)vc;
        if (svc.viewControllers.count > 0) {
            return [UIViewController topViewController:svc.viewControllers.lastObject];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UINavigationController class]] ||
               [vc isKindOfClass:[MSNavigationController class]]) {
        UINavigationController *navC = (UINavigationController*)vc;
        if (navC.viewControllers.count > 0) {
            return [UIViewController topViewController:navC.topViewController];
        } else {
            return vc;
        }
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabC = (UITabBarController*)vc;
        if (tabC.viewControllers.count > 0) {
            return [UIViewController topViewController:tabC.selectedViewController];
        } else {
            return vc;
        }
    } else {
        return vc;
    }
}
+ (UIViewController*)currentViewController {
    //    UIViewController *viewController = [MGMMainTabBarController mainTabBarSingleton];
    AppDelegate *appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
    return [UIViewController topViewController:appDelegate.window.rootViewController];
}

@end
